/**
 * @Author: boyyang
 * @Date: 2022-01-31 16:22:34
 * @LastEditTime: 2022-02-05 15:38:40
 * @LastEditors: boyyang
 * @Description: 
 * @FilePath: \boyyang\functions\thumbsUp\index.js
 */

const cloudbase = require("@cloudbase/node-sdk")
const app = cloudbase.init({
    env: 'boyyang-8gv2s8f21447c0c6'
})

const db = app.database()
const _ = db.command
exports.main = async (event, context) => {

    let response = {

    }

    let { id, uid, collectionName } = event
    // usersBlog wallpaper_mobile wallpaper_pc

    let { data } = await db.collection(collectionName).doc(id).get()

    if (data[0].thumbArray && data[0].thumbArray.includes(uid)) {
        response.msg = '今日已经点过赞啦，请明日再来'
    } else {
        let res = await db.collection(collectionName).doc(id).update({
            thumb: _.inc(1),
            thumbArray: _.push(uid)
        })

        response.msg = '点赞成功'
    }


    return response
}
